﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DHQGHN.DAL.Base;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.Models.Admin;

namespace DHQGHN.DAL.DAO.Admin
{
    public class LogDAO : SqlDAO
    {
        public const string SP_GET_LIST_PAGING = "spGetListPagingLog";        
        public const string SP_GET_BY_ID = "spGetByIdLog";
        public const string SP_DELETE = "spDeleteLog";
        public const string SP_CREATE = "spCreateLog";

        public const string PARAM_ID = "@ID";
        public const string PARAM_ACCOUNT = "@Account";
        public const string PARAM_SOFTWARE = "@SoftWare";
        public const string PARAM_FUNCTION = "@Function";
        public const string PARAM_DATA = "@Data";
        public const string PARAM_ACTION = "@Action";
        public const string PARAM_PAGE_INDEX = "@PageIndex";
        public const string PARAM_PAGE_SIZE = "@PageSize";
        public const string PARAM_START_DATE = "@StartDate";
        public const string PARAM_END_DATE = "@EndDate";
        public const string PARAM_TABLE_ID = "@TableId";
        public const string PARAM_APP_CODE = "@AppCode";
        public const string PARAM_APP_ID = "@AppId";

        public LogDAO(string connectionString, Type mapper) :
            base(connectionString, mapper) { }

        public DataPage<Log> GetDynamicListPaging(Log model, int pageIndex, int pageSize, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(LogDAO.SP_GET_LIST_PAGING);
                cmd.Parameters.Add(CreateParameter(LogDAO.PARAM_ACCOUNT, model.Account));
                cmd.Parameters.Add(CreateParameter(LogDAO.PARAM_SOFTWARE, model.SoftWare));
                cmd.Parameters.Add(CreateParameter(LogDAO.PARAM_FUNCTION, model.Function));                
                cmd.Parameters.Add(CreateParameter(LogDAO.PARAM_ACTION, model.Action));
                cmd.Parameters.Add(CreateParameter(LogDAO.PARAM_PAGE_INDEX, pageIndex));
                cmd.Parameters.Add(CreateParameter(LogDAO.PARAM_PAGE_SIZE, pageSize));
                cmd.Parameters.Add(CreateParameter(LogDAO.PARAM_START_DATE, model.StartDate));
                cmd.Parameters.Add(CreateParameter(LogDAO.PARAM_END_DATE, model.EndDate));
                cmd.Parameters.Add(CreateParameter(LogDAO.PARAM_APP_ID, model.AppId));

                return GetDataPage<Log>(cmd, pageIndex, pageSize, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public Log GetById(long id, Dictionary<string,string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(LogDAO.SP_GET_BY_ID);
                cmd.Parameters.Add(CreateParameter(LogDAO.PARAM_ID, id));

                return GetSingle<Log>(cmd, mapDictionary);
            }
            catch
            {                
                throw;
            }
        }

        public bool Save(Log model)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(LogDAO.SP_CREATE);
                cmd.Parameters.Add(CreateParameter(LogDAO.PARAM_ACCOUNT, model.Account));
                cmd.Parameters.Add(CreateParameter(LogDAO.PARAM_SOFTWARE, model.SoftWare));
                cmd.Parameters.Add(CreateParameter(LogDAO.PARAM_FUNCTION, model.Function));
                cmd.Parameters.Add(CreateParameter(LogDAO.PARAM_DATA, model.Data));
                cmd.Parameters.Add(CreateParameter(LogDAO.PARAM_ACTION, model.Action));
                cmd.Parameters.Add(CreateParameter(LogDAO.PARAM_APP_CODE, model.AppCode));

                int result = ExecuteNonQuery(cmd);

                return result > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        public bool Delete(string[] listId)
        {
            try
            {
                DataTable tableId = GetTableId(listId);
                SqlCommand cmd = this.GetSprocCommand(LogDAO.SP_DELETE);
                cmd.Parameters.Add(new SqlParameter(LogDAO.PARAM_TABLE_ID, tableId));

                int result = ExecuteNonQuery(cmd);

                return result > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        private DataTable GetTableId(string[] listId)
        {
            DataTable table = new DataTable();
            table.Columns.Add("Id");
            int len = listId.Length;
            for (int i = 0; i < len; i++)
            {
                table.Rows.Add(listId[i]);
            }

            return table;
        }
    }
}
